using System;
using System.Collections.Generic;
using System.Text;

using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;

using Athena.Objects;

namespace Athena.Tools
{
    public class TextEditTool : INonVisualTool 
    {

        private TextEditTool()
        {
            // no items to put into global tool window
            _bHasMenuItems = false;
            // has items to put into context menu
            _bHasContextMenuItems = true;
            // needs a separate toolstrip for the arrange actions
            _bRequiresNewToolWindow = false;

            // register with main instance
            ToolManager.Instance.RegisterTool("Edit Text Tool", "Edits the text of a given glyph.", this);
        }
        public override bool Handles(List<Type> oTypes)
        {
            bool bResult = true;
            foreach (Type t in oTypes)
            {
                bResult &= Handles(t);
                if (!bResult) return false;
            }
            return bResult;
        }

        private bool Handles(Type t)
        {            
            return (Utility.IsType(typeof(TextGlyph), t));            
        }
        public override void OnKeyDown(KeyEventArgs args)
        {
            switch (args.KeyCode)
            {
                case Keys.Return:
                    args.Handled = ShowTextBox(new Point(10, 10));
                    break;
                default:
                    break;
            }
        }

        private bool ShowTextBox(Point oLocation )
        {
            if (_oBox != null)
                HideTextBox();
            List<IDrawable> drawables = CurrentModel.Selected;
            if (drawables.Count == 1)
            {
                if (drawables[0] is IGlyph)
                {
                    IGlyph glyph = (IGlyph)drawables[0];
                    Font oFont = glyph.TextFont;
                    RectangleF oBounds = glyph.LabelBounds;                    
                    ShowTextBox(oFont, oBounds, glyph, oLocation);
                    return true;
                }
            }
            return false;
        }

        public override void OnItemDoubleClick(HandledMouseEventArgs e)
        {
            Point oLocation = e.Location;
            e.Handled = ShowTextBox(oLocation);
        }

        private void ShowTextBox(Font oFont, RectangleF oBounds, IGlyph oGlyph, Point point)
        {
            if (_oBox != null)
                HideTextBox();

            _oBox = new TextBox();
            _oBox.Text = oGlyph.Name;
            _oBox.Font = oFont;
            PointF oPoint = oGlyph.Location;
            float fScale = Utility.CurrentScale;
            PointF oOffset = Utility.CurrentOffset;
            
            oPoint = new PointF(oPoint.X * fScale, oPoint.Y * fScale);
            oPoint = new PointF(oPoint.X - oOffset.X, oPoint.Y - oOffset.Y);

            _oBox.Location = Point.Round(oPoint);
            _oBox.Size = new Size(Math.Max(_oBox.Size.Width, (int)oGlyph.Dimensions.Width),
                Math.Max(_oBox.Size.Height, (int)oGlyph.Dimensions.Height));
            //_oBox.Multiline = true;
            _oBox.BorderStyle = BorderStyle.None;
            
            //_oBox.TextAlign = HorizontalAlignment.Center;
            _oBox.SelectAll();
            _oBox.TextChanged += new EventHandler(_oBox_TextChanged);
            _oBox.KeyPress += new KeyPressEventHandler(_oBox_KeyPress);
            _oBox.Leave += new EventHandler(_oBox_Leave);
            _oGlyph = oGlyph;
            
            //DrawPanel oPanel = Utility.GetDrawPanel();            
            //oPanel.Controls.Add(_oBox);

            VisualCanvas canvas = Utility.GetCanvas();
            canvas.Controls.Add(_oBox);

            _oBox.Focus();
        }

        void _oBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                Utility.GetCanvas().Focus();                
                e.Handled = true;
            }
        }

        void _oBox_Leave(object sender, EventArgs e)
        {
            HideTextBox();
        }

        void _oBox_TextChanged(object sender, EventArgs e)
        {
            //HistoryManager.History.SetControllPoint("Renaming glyph ...");
            //_oGlyph.Label = _oBox.Text;
            //Utility.Refresh();
        }


        private void HideTextBox()
        {
            if (_oBox != null)
            {
                //DrawPanel oPanel = Utility.GetDrawPanel();
                //oPanel.Controls.Remove(_oBox);
                VisualCanvas canvas = Utility.GetCanvas();
                canvas.Controls.Remove(_oBox);
                _oBox.TextChanged -= _oBox_TextChanged;
                _oBox.KeyPress -= _oBox_KeyPress;
                _oBox.Leave -= _oBox_Leave;
                /*if (_oGlyph is PartGlyph)
                {
                    _oGlyph.Name = _oBox.Text;
                }*/
                _oGlyph.Name = _oBox.Text;
                _oBox = null;
                Utility.Refresh();

                
            }
        }

        IGlyph _oGlyph = null;
        TextBox _oBox = null;

    }
}
